package com.makemoney.miniprogram.modules.users.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.makemoney.miniprogram.modules.users.dao.IUsersDao;
import com.makemoney.miniprogram.modules.users.entity.Users;
import com.makemoney.miniprogram.modules.users.mapper.UsersMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class UsersDaoImpl extends ServiceImpl<UsersMapper, Users> implements IUsersDao {

    @Override
    public Users findUserByUsername(String userName) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Users::getUserName, userName)
                .last("LIMIT 1");
        return this.getOne(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateLoginTime(Long userId){
        UpdateWrapper<Users> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .set(Users::getLastLoginTime, LocalDateTime.now())
                .eq(Users::getId,userId);
        return this.update(wrapper);
    }
}
